package com.example.shaidemo;

import androidx.appcompat.app.AlertDialog;
import androidx.appcompat.app.AppCompatActivity;

import android.content.DialogInterface;
import android.content.Intent;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.AdapterView;
import android.widget.ListView;
import android.widget.SimpleAdapter;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

public class CollectActivity extends AppCompatActivity implements AdapterView.OnItemLongClickListener {
    ListView listView;
    SimpleAdapter simpleAdapter;
    List<Map<String,String>> collectList;
    private static final String TAG = "CollectActivity";


    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_collect);

        listView = findViewById(R.id.collectList_label);
        collectList = GetData.getData();
        simpleAdapter = new SimpleAdapter(this,collectList,
                R.layout.collect_list_view,
                new String[]{"title","company","majority","salary","img"},
                new int[]{R.id.title_label,R.id.uploadTime_label,R.id.uploadText_1,R.id.uploadText_2,R.id.collect_view});
        listView.setAdapter(simpleAdapter);
        //绑定事件处理
        listView.setOnItemLongClickListener(this::onItemLongClick);
    }

    @Override
    public boolean onItemLongClick(AdapterView<?> adapterView, View view, int position, long l) {
        Log.i(TAG, "onItemLongClick: 长按列表项position=" + position);

        Object itemAtPosition = listView.getItemAtPosition(position);
        //删除操作
        //构造对话框进行确认操作
        AlertDialog.Builder builder = new AlertDialog.Builder(this);
        builder.setTitle("提示").setMessage("请确认是否删除当前数据?").setPositiveButton("是",new DialogInterface.OnClickListener(){

            @Override
            public void onClick(DialogInterface dialog, int which) {
                Log.i(TAG, "onClick: 对话框事件处理");
                if(collectList.remove(position)!=null){
                    System.out.println("success");
                }else{
                    System.out.println("failed");
                }
                simpleAdapter.notifyDataSetChanged();
            }
        }).setNegativeButton("否",null);
        builder.create().show();
        Log.i(TAG, "onItemLongClick: size=" + collectList.size());

        return true;
    }
}